<?
  /* File:    lib/xml/xml2array-selectbyxpath.php: provides a function to
   *          select elements of a parsed XML structure by simple XPath statements
   * Type:    CMS function library
   * Author:  udo.schroeter@gmail.com
   * License: commercially licensed as part of the CMS package
   * Todo:    /
   * Changes: -
   */

function _selectByXPathEx(&$srcarray, $path, $createIfNotExists, $insertnode)
{
  // make an array out of the XPath
  $pathar = explode('/', $path);
  $lauf = &$srcarray;
  $pathcount = sizeof($pathar);
  $pathctr = 0;
  if (is_array($pathar))
  {
    foreach ($pathar as $entry)
    {
      $pathctr++;
      // ignore empty statements
      if ($entry!='')
      {
        if ($entry=='@')
        {
          $attrib = $lauf['@'];
          if (!isset($attrib) && $createIfNotExists)
          {
            // create the attribute if it doesn't exists
            $lauf['@'] = array();
          }
          $lauf = &$attrib;
          return($attrib);
        }
        else if (substr($entry, 0, 1)=='@')
        {
          // return the attribute
          $attrib = &$lauf['@'][substr($entry, 1)];
          if (!isset($attrib) && $createIfNotExists)
          {
            // create the attribute if it doesn't exists
            if (!isset($lauf['@'])) $lauf['@'] = array();
            $lauf['@'][substr($entry, 1)] = '';
            $lauf = &$lauf['@'][substr($entry, 1)];
            return $lauf['@'][substr($entry, 1)];
          }
          $lauf = &$attrib;
          return($attrib);
        }
        else if ($entry == '$')
        {
          // return string content
          $stringContent = &$lauf['$'];
          if (!isset($lauf) && $createIfNotExists)
          {
            $lauf['$'] = '-';
          }
          $lauf = &$stringContent;
          return($stringContent);
        }
        else
        {
          // handle element
          if (substr($entry, -1)== ']')
          {
            // get the index as specified
            $f1 = strrpos($entry, '[');
            $entryindex = substr($entry, $f1+1, -1);
            $entry = substr($entry, 0, $f1);
          }
          else
            $entryindex = 0;
          // get the next element from the tree
          $nextElement = &$lauf[$entry][$entryindex];
          // cancel if node not found
          if (!isset($nextElement))
          {
            if ($createIfNotExists)
            {
              $nextElement = array();
              $lauf[$entry][$entryindex] = &$nextElement;
              //print('create entry '.$entry.' '.$entryindex."\n");
            }
            else
            {
              $lauf = &$nextElement;
              return($nextElement);
            }
          }
          if ($pathctr==$pathcount && is_array($insertnode))
          {
            // insert the insertnode here
            $lauf[$entry][$entryindex] = &$insertnode;
          }
          // move further along in the array
          $lauf = &$nextElement;
        }
      }
    }
  }
  return($lauf);
}
?>
